#include "Resource.h"


log4cxx::LoggerPtr Resource::_logger = log4cxx::Logger::getLogger("Resource");

Resource::Resource() {
	_last_acquire_time = -1;
	_last_release_time = -1;
	_is_in_use = false;
	_index = -1;
	_flag = 0;
}

Resource::~Resource() {}

time_t Resource::get_last_acquire_time() { return _last_acquire_time; }

time_t Resource::get_last_release_time() { return _last_release_time; }

int Resource::acquire() {
	if (_is_in_use) {
		LOG4CXX_ERROR(_logger, "acquire a resource that is in use, index:" << _index);
		return -1;
	}
	_is_in_use = true;
	_last_acquire_time  = time(NULL);
	return 1;
}

int Resource::set_index(int index) {
		_index = index;
		return 1;
}

int Resource::get_index() {
		return _index;
}


int Resource::release() {
	if (!_is_in_use) {
		LOG4CXX_WARN(_logger, "release a resource that it not in use, index:" << _index);
		return -1;
	}
	_is_in_use = false;
	_last_release_time = time(NULL);
	return 1;
}

bool Resource::is_in_use() { return _is_in_use; }

int Resource::set_flag(int flag) { _flag = flag; return 1; }

int Resource::get_flag() { return _flag; }
